package walasp.proto;

import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Helper Class used to get store update and remove tasks from the applications SQLite Database
 *
 */
public class TaskHelper {

	TaskDBHelper dbClass;
	SQLiteDatabase TaskDB;

	/**
	 * Creates a new TaskHelper instance
	 * @param c
	 * 		The Context
	 */
	public TaskHelper(Context c) {
		dbClass = new TaskDBHelper(c);
		TaskDB = dbClass.getWritableDatabase();
	}
	
	/**
	 * Closes the database connection to avoid leaking them. Should be called
	 * after the helper is no longer needed.
	 */
	public void close() {
		dbClass.close();
	}

	/**
	 *  Gets a Task based on TID
	 * @param TID the task id of the Task to get
	 * @return Task instance or null if invalid TID
	 */
	public Task getTask(int TID) {
		// check if guard value
		if (TID == 0) {
			return null;
		}
		String TIDstr = String.valueOf(TID);

		String[] selectArgs = { TIDstr };
		Cursor results = TaskDB.rawQuery("Select * from "+TaskDBHelper.TASK_TABLE_NAME +" Where TID = ?",
				selectArgs);
		results.moveToFirst();
		return new Task(
				results.getInt(Task.TASK_COLUMN_INT_TID),
				results.getInt(Task.TASK_COLUMN_INT_PARENTTID),
				results.getString(Task.TASK_COLUMN_INT_TASKNAME),
				results.getString(Task.TASK_COLUMN_INT_TASKCONTENT),
				results.getInt(Task.TASK_COLUMN_INT_PRIORITY),
				results.getString(Task.TASK_COLUMN_INT_STATUS),
				results.getString(Task.TASK_COLUMN_INT_CONTACTS),
				new Date(results
						.getLong(Task.TASK_COLUMN_INT_CREATIONDATE)),
				new Date( results.getLong(Task.TASK_COLUMN_INT_STARTDATE)),
				new Date(results.getLong(Task.TASK_COLUMN_INT_DUEDATE)),
				new Date(results
						.getLong(Task.TASK_COLUMN_INT_COMPLTEDDATE)));

	}

	/**
	 * Get a list of all Tasks in the database
	 * @return an ArrayList<Task> containing all Tasks in the database
	 */
	public ArrayList<Task> getAllTasks() {
		Cursor results = TaskDB.rawQuery("Select * from TASKS", null);
		ArrayList<Task> tasks = new ArrayList<Task>();
		// results.moveToFirst(); shouldnt be needed
		while (results.moveToNext()) {

			tasks.add(new Task(results));

		}
		results.close();
		return tasks;
	}
	
	/**
	 * Gets the maximum TID in the database
	 * @return the max TID in the database or 0 if the database is empty
	 */
	public int getMaxTID(){
		Cursor Results = TaskDB.rawQuery("select MAX(TID) from Tasks",null);
		Results.moveToFirst();
		Integer max = Integer.valueOf(Results.getInt(0));
		Results.close();
		if (max == null) return 0;
		else return max;
		
	}
	
	/**
	 * Get the next available TID to be used on the next Task that is created.
	 * @return the next available TID
	 */
	public int getNextTID(){
		return (getMaxTID() +1);
	}

	/**
	 * Deletes a Task from the Task database.
	 * @param TID
	 * 			The TID of the Task to be deleted
	 */
	public void deleteTask(int TID) {
		String TIDstr = String.valueOf(TID);

		String[] selectArgs = { TIDstr };
		TaskDB.delete(TaskDBHelper.TASK_TABLE_NAME, "TID=?", selectArgs);
		
	}
	
	/**
	 * Inserts a task into the Task database.
	 * @param t
	 * 			The Task to be inserted into the database
	 * @return true on successful insertion, false otherwise
	 */
	public boolean insertTask(Task t) {
		ContentValues content = new ContentValues();
		content.put(Task.TASK_COLUMN_TID, t.getTID());
		content.put(Task.TASK_COLUMN_PARENTTID, t.getpTID());
		content.put(Task.TASK_COLUMN_COMPLTEDDATE, t.getCompletedDate().getTime());
		content.put(Task.TASK_COLUMN_CONTACTS, t.getContacts());
		content.put(Task.TASK_COLUMN_CREATIONDATE, t.getCreationDate().getTime());
		if (t.getDueDate() != null) {
			content.put(Task.TASK_COLUMN_DUEDATE, t.getDueDate().getTime());
		} else {
			content.put(Task.TASK_COLUMN_DUEDATE, 0);
		}
		content.put(Task.TASK_COLUMN_PRIORITY, t.getPriority());
		content.put(Task.TASK_COLUMN_STARTDATE, t.getStartDate().getTime());
		content.put(Task.TASK_COLUMN_STATUS, t.getStatus());
		content.put(Task.TASK_COLUMN_TASKCONTENT, t.getTaskContent());
		content.put(Task.TASK_COLUMN_TASKNAME,t.getTaskName());
		
		if ((TaskDB.insert("TASKS", null, content)) != -1) {
			return true;
		} else {
			return false;
		}

	}
	
	/**
	 * Updates a Task in the database with the values of the given Task.
	 * @param t
	 * 			The Task to be updated.
	 * @return true on successful update, false otherwise.
	 */
	public boolean updateTask(Task t) {
		ContentValues content = new ContentValues();
		//content.put(Task.TASK_COLUMN_TID, t.getTID());
		content.put(Task.TASK_COLUMN_PARENTTID, t.getpTID());
		content.put(Task.TASK_COLUMN_COMPLTEDDATE, t.getCompletedDate().getTime());
		content.put(Task.TASK_COLUMN_CONTACTS, t.getContacts());
		content.put(Task.TASK_COLUMN_CREATIONDATE, t.getCreationDate().getTime());
		if (t.getDueDate() != null) {
			content.put(Task.TASK_COLUMN_DUEDATE, t.getDueDate().getTime());
		} else {
			content.put(Task.TASK_COLUMN_DUEDATE, 0);
		}
		content.put(Task.TASK_COLUMN_PRIORITY, t.getPriority());
		content.put(Task.TASK_COLUMN_STARTDATE, t.getStartDate().getTime());
		content.put(Task.TASK_COLUMN_STATUS, t.getStatus());
		content.put(Task.TASK_COLUMN_TASKCONTENT, t.getTaskContent());
		content.put(Task.TASK_COLUMN_TASKNAME,t.getTaskName());
		String args[] = {
				 String.valueOf(t.getTID())
		
		};
		if ((TaskDB.update("TASKS", content, "TID= ?", args)) == 1) {
			return true;
		} else {
			
			//uhoh
			return false;
		}

	}
	


}
